﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Controls.Maps;
using System.Device.Location;
using System.IO.IsolatedStorage;

namespace WindowsPhoneApplication1
{
    public partial class mapPage : PhoneApplicationPage
    {

        GeoCoordinateWatcher myCoordinateWatcher;
        double Latitued, Longitude;
        String trkName;
        IsolatedStorageSettings trkData = IsolatedStorageSettings.ApplicationSettings;
        String[] trks;// = { "aaa,beer sheva,hard,no,34.901426,31.263145,Images/waterfall_pool.jpg,bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla",
                        //  "bbb,beer sheva,easy,yes,34.801426,31.163145,Images/waterfall_pool.jpg,bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla",
                          //"ccc,beer sheva,easy,no,34.701426,31.363145,Images/waterfall_pool.jpg,bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla"};
        
        public mapPage()
        {
            InitializeComponent();
        }

        private void myCoordinateWatcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
        {
            if (!e.Position.Location.IsUnknown)
            {
                Latitued = e.Position.Location.Latitude;
                Longitude = e.Position.Location.Longitude;
                map1.Center = new GeoCoordinate(Latitued, Longitude);
            }
        }

        //recommended track
        private void handeleRecTrk()
        {
            double trkLatitued=0, trkLongitude=0;

            if (trkData.Contains("longitude"))
                trkLongitude = (double)trkData["longitude"];

            if (trkData.Contains("latitude"))
                trkLatitued = (double)trkData["latitude"];

            if (trkData.Contains("name"))
            {
                trkName = (String)trkData["name"];

                Pushpin trkPushpin = new Pushpin();
                //trkPushpin.MouseEnter += new MouseEventHandler(locationPushpin_MouseEnter);
                trkPushpin.Background = new SolidColorBrush(Colors.Green);
                trkPushpin.Content = trkName;
                trkPushpin.Tag = "trkPushpin";
                trkPushpin.Location = new GeoCoordinate(trkLatitued, trkLongitude);
                this.map1.Children.Add(trkPushpin);
                map1.Center = new GeoCoordinate(trkLatitued, trkLongitude);
            }


        }

        //search by location multi tracks
        private void handleSrcByLoc(String[] trks)
        {
            double longitude,latitude;

            for (int i = 0; i < trks.Length; i++)
            {
                
                //parse
                longitude = Double.Parse(trks[i].Split(',')[4]);
                latitude = Double.Parse(trks[i].Split(',')[5]);

                //show pushpin
                Pushpin trkPushpin = new Pushpin();
                trkPushpin.MouseEnter += new MouseEventHandler(locationPushpin_MouseEnter);
                trkPushpin.Background = new SolidColorBrush(Colors.Green);
                trkPushpin.Content = trks[i].Split(',')[0];
                trkPushpin.Tag = "trkPushpin";
                trkPushpin.Location = new GeoCoordinate(latitude, longitude);
                this.map1.Children.Add(trkPushpin);

            }
        }

        private void loadMyLocation(object sender, RoutedEventArgs e)
        {

            Longitude = 34.801426;
            Latitued = 31.263145;
            map1.Center = new GeoCoordinate(Latitued, Longitude);

            if (trkData.Contains("sourcePg"))
            {

                if ((String)trkData["sourcePg"] == "recTrk")
                {

                    handeleRecTrk();

                }

                if ((String)trkData["sourcePg"] == "srcByLoc")
                {
                    WindowsPhoneApplication1.ServiceReference1.Service1Client client;
                    client = new WindowsPhoneApplication1.ServiceReference1.Service1Client();
                    client.searchByLocationCompleted += new EventHandler<ServiceReference1.searchByLocationCompletedEventArgs>(client_searchByLocationCompleted);
                    client.searchByLocationAsync("", Latitued.ToString(), Longitude.ToString());                  
                }

            }

            map1.ZoomLevel = 10;
            map1.ZoomBarVisibility = Visibility.Visible;
            myCoordinateWatcher = new GeoCoordinateWatcher(GeoPositionAccuracy.Default);
            myCoordinateWatcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(myCoordinateWatcher_PositionChanged);

            Pushpin locationPushpin = new Pushpin();
            locationPushpin.Background = new SolidColorBrush(Colors.Purple);
            locationPushpin.Content = "You";
            locationPushpin.Tag = "locationPushpin";
            locationPushpin.Location = myCoordinateWatcher.Position.Location;
            locationPushpin.Location = new GeoCoordinate(Latitued, Longitude);

            this.map1.Children.Add(locationPushpin);
            //this.map1.SetView(myCoordinateWatcher.Position.Location,18.0);

        }

        void client_searchByLocationCompleted(object sender, ServiceReference1.searchByLocationCompletedEventArgs e)
        {
            if (e.Error == null && e.Result != "")
            {
               trks = e.Result.Split(';');
               handleSrcByLoc(trks);
            }
            else
            {
                MessageBox.Show("DB Error");
            }
        }

        void locationPushpin_MouseEnter(object sender, MouseEventArgs e)
        {
            Microsoft.Phone.Controls.Maps.Pushpin tempPP = new Microsoft.Phone.Controls.Maps.Pushpin();

            tempPP = (Microsoft.Phone.Controls.Maps.Pushpin)sender;

            for (int i=0; i<trks.Length; i++)
            {
                if (tempPP.Content.ToString() == trks[i].Split(',')[0])
                {
                    
                    if (!trkData.Contains("name"))
                        trkData.Add("name", trks[i].Split(',')[0]);
                    else
                        trkData["name"] = trks[i].Split(',')[0];

                    if (!trkData.Contains("location"))
                        trkData.Add("location", trks[i].Split(',')[1]);
                    else
                        trkData["location"] = trks[i].Split(',')[1];

                    if (!trkData.Contains("difficulty"))
                        trkData.Add("difficulty", trks[i].Split(',')[2]);
                    else
                        trkData["difficulty"] = trks[i].Split(',')[2];

                    if (!trkData.Contains("accessibility"))
                        trkData.Add("accessibility", trks[i].Split(',')[3]);
                    else
                        trkData["accessibility"] = trks[i].Split(',')[3];

                    if (!trkData.Contains("longitude"))
                        trkData.Add("longitude", Double.Parse(trks[i].Split(',')[4]));
                    else
                        trkData["longitude"] = Double.Parse(trks[i].Split(',')[4]);

                    if (!trkData.Contains("latitude"))
                        trkData.Add("latitude", Double.Parse(trks[i].Split(',')[5]));
                    else
                        trkData["latitude"] = Double.Parse(trks[i].Split(',')[5]);

                    if (!trkData.Contains("picPath"))
                        trkData.Add("picPath", trks[i].Split(',')[6]);
                    else
                        trkData["picPath"] = trks[i].Split(',')[6];

                    if (!trkData.Contains("description"))
                        trkData.Add("description", trks[i].Split(',')[7]);
                    else
                        trkData["description"] = trks[i].Split(',')[7];


                    if (!trkData.Contains("SotredParams"))
                        trkData.Add("SotredParams", "yes");
                    else
                        trkData["SotredParams"] = "yes";
                    

                    trkData.Save();

                    NavigationService.Navigate(new Uri("/trackPage.xaml", UriKind.RelativeOrAbsolute));
                    
                }

            }

            NavigationService.Navigate(new Uri("/trackPage.xaml", UriKind.RelativeOrAbsolute));
        }

        
        private void PhoneApplicationPage_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
        {
            trkData.Remove("sourcePg");
        }

    }

}